package com.hoyo.web;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.hoyo.config.UserRealm.Principal;
import com.hoyo.domain.User;
import com.hoyo.domain.UserInfo;
import com.hoyo.service.UserService;

@Controller
public class LoginController {
	
	@Autowired
	private UserService userService;
	
	/**
	 * Go login.jsp
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="login", method=RequestMethod.GET)
	public ModelAndView login() throws Exception {
		ModelAndView mav = new ModelAndView("login.html");
		/*Principal Principal = (Principal) SecurityUtils.getSubject().getPrincipal();
		if(Principal!=null){
			User user = userService.findByIdentifier(Principal.getTelephone());
			UserInfo userInfo = userService.findUserInfoByUserId(user.getId());
			mav.addObject("user", user);
			mav.addObject("userInfo", userInfo);
		}*/
		return mav;
	}
	
	/**
	 * Go login
	 * @param request
	 * @return
	 */
	@RequestMapping(value="login", method=RequestMethod.POST)
	public String login(@RequestParam("account") String account, @RequestParam("password") String password
			, RedirectAttributes rediect,HttpSession session) {
		System.out.println("account:"+account);
		UsernamePasswordToken upt = new UsernamePasswordToken(account, password);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(upt);
			Principal principal = (Principal) subject.getPrincipal();
			session.setAttribute("user", principal.getUser());
			session.setAttribute("userInfo", principal.getUserInfo());
			if(subject.hasRole("admin")){
				return "redirect:/admin/";
			}else{
				return "redirect:/";
			}
		} catch (AuthenticationException e) {
			e.printStackTrace();
			rediect.addFlashAttribute("errorMsg", "您的账号或密码输入错误!");
			return "redirect:/login";
		}
	}
	
	/**
	 * Exit
	 * @return
	 */
	@RequestMapping("logout")
	public String logout(HttpSession session) {
		Subject subject = SecurityUtils.getSubject();
		session.setAttribute("user", null);
		session.setAttribute("userInfo", null);
		subject.logout();
		return "redirect:/index";
	}
	
/*	@RequestMapping("/vue")
	@ResponseBody
	public String vue() {
		JSONObject obj = new JSONObject();
		obj.put("message", "bbbb");
		return obj.toString();
	}*/
	
}
